package neusoft.yuejian_11.model;

import java.util.List;

import neusoft.yuejian_11.bean.AttentionBean;
import neusoft.yuejian_11.bean.IsTrainn;
import neusoft.yuejian_11.bean.TrainBean;
import neusoft.yuejian_11.bean.TrainnBean;
import neusoft.yuejian_11.bean.TrainnInBean;
import neusoft.yuejian_11.interF.AttentionService;
import neusoft.yuejian_11.interF.Const;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.interF.RetrofitService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by neusoft on 2017/9/29.
 */

public class TrainModel {
    private Retrofit retrofit;
    public TrainModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //培训列表
    public void trainList(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<TrainBean>> call = retrofitService.getAllTrains();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeanList = response.body();
                listener.onSuccess(trainBeanList,1);
            }

            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();

            }
        };
        call.enqueue(callback);
    }
    //培训详情
    public void trainDetail(String trainId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call = retrofitService.getTrainById(trainId);
        Callback<TrainBean> callback = new Callback<TrainBean>() {
            @Override
            public void onResponse(Call<TrainBean> call, Response<TrainBean> response) {
                TrainBean trainBeanDetail = response.body();
                listener.onSuccess(trainBeanDetail,1);
            }

            @Override
            public void onFailure(Call<TrainBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //搜索培训
    public void trainSearch(String trainName, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<TrainBean>> call = retrofitService.getTrainBySearch(trainName);
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeanList = response.body();
                listener.onSuccess(trainBeanList,1);
            }

            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //判断是否报名
    public void IsAddTrainn(int userId, String trainId,final RetrofitListener listener) {
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call=retrofitService.IsAddTrainn(userId,String.valueOf(trainId));
        call.enqueue(new Callback<IsTrainn>() {
            @Override
            public void onResponse(Call<IsTrainn> call, Response<IsTrainn> response) {
                IsTrainn isAddTrainn=response.body();
                listener.onSuccess(isAddTrainn,5);
            }
            @Override
            public void onFailure(Call<IsTrainn> call, Throwable t) {

            }
        });
    }
    //培训报名
    public void trainn(int userId, int trainId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<TrainnBean> call = retrofitService.getTrainnById(userId,trainId);
        Callback<TrainnBean> callback = new Callback<TrainnBean>() {
            @Override
            public void onResponse(Call<TrainnBean> call, Response<TrainnBean> response) {
                TrainnBean trainnBean = response.body();
                listener.onSuccess(trainnBean,4);
            }

            @Override
            public void onFailure(Call<TrainnBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //收藏培训
    public void favorTrain(int userId, String trainId, final RetrofitListener listener){
        AttentionService attentionService = retrofit.create(AttentionService.class);
        Call<AttentionBean> call = attentionService.userCollectTrain(userId,trainId);
        Callback<AttentionBean> callback = new Callback<AttentionBean>() {
            @Override
            public void onResponse(Call<AttentionBean> call, Response<AttentionBean> response) {
                AttentionBean attentionBean = response.body();
                listener.onSuccess(attentionBean,2);
            }

            @Override
            public void onFailure(Call<AttentionBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //培训报名列表
    public void trainnInList(int userId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<TrainnInBean>> call = retrofitService.getTrainReg(userId);
        Callback<List<TrainnInBean>> callback = new Callback<List<TrainnInBean>>() {
            @Override
            public void onResponse(Call<List<TrainnInBean>> call, Response<List<TrainnInBean>> response) {
                List<TrainnInBean> trainnInBeanList = response.body();
                listener.onSuccess(trainnInBeanList,1);
            }

            @Override
            public void onFailure(Call<List<TrainnInBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //进阶培训列表
    public void  getJinjieTrains(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<TrainBean>> call = retrofitService.getJinjieTrains();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBean = response.body();
                listener.onSuccess(trainBean,1);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }



    //菜鸟培训列表
    public void  getCainiaoTrains(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<TrainBean>> call = retrofitService.getCainiaoTrains();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBean = response.body();
                listener.onSuccess(trainBean,1);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }



    //入门培训列表
    public void  getRumenTrains(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<TrainBean>> call = retrofitService.getRumenTrains();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBean = response.body();
                listener.onSuccess(trainBean,1);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }



    //高级培训列表
    public void  getGaijiTrains(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<TrainBean>> call = retrofitService.getgaojiTrains();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBean = response.body();
                listener.onSuccess(trainBean,1);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

}